﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class LogDataDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static LogDataDataAccess Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="AddressDataAccess" /> class.
        /// </summary>
        static LogDataDataAccess() { Instance = new LogDataDataAccess(); }

        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static LogData ExtractRawData(IDataRecord dataReader)
        {
            var logData = new LogData
                              {
                                  LogId = (Guid)dataReader["LogId"],
                                  LogDate = (DateTime)dataReader["LogDate"],
                                  IdRecord = (Guid)dataReader["IdRecord"],
                                  SessionId = dataReader["SessionId"].ToString()
                              };
            return logData;


        }
        /// <summary>
        /// Gets the log data.
        /// </summary>
        /// <returns></returns>
        public Collection<LogData> GetLogData()
        {


            var logData = new Collection<LogData>();
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.ListLogData))
            {
                while (reader.Read())
                {
                    logData.Add(ExtractRawData(reader));

                }
                return logData;
            }
        }

        /// <summary>
        /// Inserts the specified log data.
        /// </summary>
        /// <param name="logData">The log data.</param>
        /// <returns></returns>
        public int Insert(LogData logData)
        {
            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("LogId", logData.LogId),
                                        new SqlParameter("LogDate", logData.LogDate),
                                         new SqlParameter("IdRecord", logData.IdRecord),
                                          new SqlParameter("SessionId",logData.SessionId)
                                   };
            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.InsertLogData, colParameter);
            return resuft;
        }
        /// <summary>
        /// Deletes the log data by session.
        /// </summary>
        /// <param name="sessionId">The session id.</param>
        /// <returns></returns>
        public int DeleteLogDataBySession(string sessionId)
        {

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("sessionId", sessionId)
                                   };
            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.DeleteLogData, colParameter);
            return resuft;
        }
       
    }
}
